#!/bin/bash

if [ "$1" == "help" ];then
   
   #下载文件
   wget https://gitee.com/wang-daming-faith/xmshell/raw/master/help.txt -O help.txt     
   # 查询判断
   if [ "$2" == "" ];then
      cat help.txt
   else
      cat help.txt | grep $2
   fi

   #rm -f 信息.txt
elif [ "$1" == "shell" ];then
   #获取当前路径
   CRTDIR=$(pwd)
   #获取当前名称
   script_name=$(basename "$0")
   read -p "$CRTDIR | XM-Shell#> " -e text
   if [ "$text" == "exit" ];then
      exit
   else
      bash $script_name $text
      bash $script_name shell
   fi

elif [ "$1" == "install" ];then #安装命令
 
    # 检查wget是否安装  
    if command -v wget >/dev/null 2>&1; then  
       echo -n ""  
    else  
       sudo apt install wget -y
    fi  
  
    # 检查curl是否安装  
   if command -v curl >/dev/null 2>&1; then  
       echo -n ""  
   else  
       sudo apt install curl -y
   fi

   #install
   if [ "$2" == "java8" ];then
      sudo apt install openjdk-8-jre-headless

    elif [ "$2" == "lx" ];then
      if [ "$3" == "java8" ];then
         #获取系统
         architecture=$(uname -m)

         #判断架构
         if [ "$architecture" == "x86_64" ];then
            javaurl="https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz"

         elif [ "$architecture" == "aarch64" ];then
            javaurl="https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-arm64-vfp-hflt.tar.gz"
        
         else
            javaurl="https://repo.huaweicloud.com/java/jdk/8u202-b08/jdk-8u202-linux-x64.tar.gz"

         fi

        # 使用curl命令下载java.tar.gz  

        #if
        if [ -f "java.tar.gz" ];then  
           echo "准备安装"
        else
           wget -O java.tar.gz $javaurl
        fi

        sudo mkdir /opt/java
        sudo mkdir /opt/java/java8
        sudo tar -xzf java.tar.gz -C /opt/java/java8
        
      else
        echo "没有安装程序"
      fi

    elif [ "$2" == "clang" ];then
       sudo apt install clang

    elif [ "$2" == "python" ];then
       sudo apt install python3

    elif [ "$2" == "golang" ];then
       sudo apt-get install golang
       go version

    # apt
    elif [ "$2" == "qinghuayuan" ];then
       sudo apt install golang

    #mianban
    elif [ "$2" == "1pane" ];then

      #判断系统安装
       if [ -f /etc/lsb-release ]; then
          curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && sudo bash quick_start.sh
       elif [ -f /etc/debian_version ]; then
          curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && bash quick_start.sh
       elif [ -f /etc/centos-release ]; then
          curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && sh quick_start.sh
       else
          echo "Unsupported system."
       fi

    elif [ "$2" == "alist" ];then
       curl -fsSL "https://alist.nn.ci/v3.sh" | bash -s install

    elif [ "$2" == "casaos" ];then
       curl -fsSL https://get.casaos.io | sudo bash

    else
       echo -n -e  "\e[31m"
       echo "没有参数"
       echo "可以使用 <help> 命令查看参数"
       echo "在 <help> 后加 <关键词> 可以快速搜索"
       echo -e "\e[0m"
    fi

elif [ "$1" == "dl" ];then #下载命令
   if [ "$2" == "s" ];then
      if [ "$3" == "" ];then
         echo -n -e  "\e[31m"
         echo "没有下载链接" 
         echo -e "\e[0m"
      else
         wget $3
      fi
    else
      if [ "$2" == "" ];then
         echo "没有下载链接"
      else
         wget $2
      fi
    fi
elif [ "$1" == "spm" ];then #网络扫描
   if [ "$2" == "ip" ];then
      ip add
   elif [ "$2" == "ifp" ];then
      ifconfig
   elif [ "$2" == "port" ];then
      ss -tuln
   else
      echo -n -e  "\e[31m"
      echo "没有参数 "
      echo "可以使用 <help> 命令查看参数"
      echo "在 <help> 后加 <关键词> 可以快速搜索"
      echo -e "\e[0m"
   fi

elif [ "$1" == "sm" ];then #系统信息

   if [ "$2" == "l" ];then 
      sudo apt install neofetch
      neofetch

   elif [ "$2" == "a" ];then 
      uname -a

   elif [ "$2" == "v" ];then
      cat /proc/version

   elif [ "$2" == "re" ];then
      cat /etc/issue

   elif [ "$2" == "pc" ];then
      lscpu   

   elif [ "$2" == "cpu" ];then
      getconf LONG_BIT

   elif [ "$2" == "hn" ];then
      hostname
       
   else
      echo -n -e  "\e[31m"
      echo "没有参数 "
      echo "可以使用 <help> 命令查看参数"
      echo "在 <help> 后加 <关键词> 可以快速搜索"
      echo -e "\e[0m"
   fi
elif [ "$1" == "java" ];then #java 执行命令
   if [ "$2" == "" ];then
      echo -n -e  "\e[31m"
      echo "没有参数"
      echo "输入java参数，例如 -version"
      echo -e "\e[0m"
   else
      /opt/java/java8/jdk1.8.0_202/bin/java ${@:2}
   fi

elif [ "$1" == "set" ];then #系统设置
   #判断第二参数
   if [ "$2" == "wp" ];then #设置代理服务器
      if [ "$3" == "" ];then
         echo -n -e  "\e[31m"
         echo "输入ip地址 如：192.168.1.3:8075"
         echo -e "\e[0m"
      else
         export http_proxy=$3
         export https_proxy=$3
      fi

   elif [ "$2" == "passwd" ];then #设置用户密码
      # 获取用户名
      username=$(whoami)
      sudo passwd $username 

   else
      echo -n -e  "\e[31m"
      echo "可以使用 <help> 命令查看参数"
      echo "在 <help> 后加 <关键词> 可以快速搜索"
      echo -e "\e[0m"
   fi

elif [ "$1" == "sut" ];then
   if [ "$2" == "" ];then
      echo -n -e  "\e[31m"
      echo "输入系统命令"
      echo -e "\e[0m"
   else
      ${@:2}
   fi

else #可以在上面加elif
   echo -n -e  "\e[31m"
   echo "错误参数"
   echo "例如 help、shell、dl等。"
   echo "使用 <help> 命令查看参数"
   echo "在 <help> 后加 <关键词> 可以快速搜索"
   echo -e "\e[0m"
fi